package com.yuhanginfo.modules.sys.utils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.yuhanginfo.common.utils.SpringContextHolder;
import com.yuhanginfo.modules.sys.dao.GoodsDao;
import com.yuhanginfo.modules.sys.dao.OrdersDao;
import com.yuhanginfo.modules.sys.dao.ReturnsGoodsDao;
import com.yuhanginfo.modules.sys.dao.MaterielDao;
import com.yuhanginfo.modules.sys.dao.TransactionHistoryDao;
import com.yuhanginfo.modules.sys.entity.ReturnsGoods;


public class CodeUtil {
	
	private static GoodsDao goodsDao = SpringContextHolder.getBean(GoodsDao.class);
	
	private static TransactionHistoryDao transactionHistoryDao = SpringContextHolder.getBean(TransactionHistoryDao.class);;
	
	private static OrdersDao ordersDao = SpringContextHolder.getBean(OrdersDao.class);
	
	private static ReturnsGoodsDao returnsGoodsDao = SpringContextHolder.getBean(ReturnsGoodsDao.class);
	
	private static MaterielDao materielDao = SpringContextHolder.getBean(MaterielDao.class);
	
	private CodeUtil(){
		
	}
	public static String getGoodsCode(){
		String format = "C%08d";
		int goodsCode = getMaxGoodsCode();
		goodsCode = goodsCode+1;
		return String.format(format, goodsCode);
	}
	

	public static String getTransactionHistoryReturnCode(){
		String format = "R%03d";
		Date nowDate = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHH");
		String now = sdf.format(nowDate);
		int transactionHistoryCode = getMaxTransactionHistoryReturnCode(now+"R");
		transactionHistoryCode = transactionHistoryCode+1;
		String s = now+format;
		return String.format(s, transactionHistoryCode);
	}
	
	public static String getMaterielCode(String prefix){
		String format = "%05d";
		int materielCode = getMaxMaterielCode(prefix);
		materielCode = materielCode+1;
		String s = prefix+format;
		return String.format(s, materielCode);
	}
	
	private static int getMaxMaterielCode(String prefix) {
		String code = materielDao.findMaxCode(prefix);
		if(code!=null){
			String substring = code.substring(code.length()-5);
			return Integer.parseInt(substring);
		}else{
			return 0;
		}
	}


	public static String getTransactionHistoryCutCode(){
		String format = "C%03d";
		Date nowDate = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHH");
		String now = sdf.format(nowDate);
		int transactionHistoryCode = getMaxTransactionHistoryCutCode(now+"C");
		transactionHistoryCode = transactionHistoryCode+1;
		String s = now+format;
		return String.format(s, transactionHistoryCode);
	}
	
	public static String getReturnsGoodsCode(String orderCode){
		ReturnsGoods query = new ReturnsGoods();
		query.setOrderCode(orderCode);
		ReturnsGoods result = returnsGoodsDao.get(query);
		if(result != null){
			return "T"+orderCode+"-"+(Integer.valueOf(result.getReturnCode().substring(result.getReturnCode().indexOf("-")+1, result.getReturnCode().length()))+1);
		}else{
			return "T"+orderCode+"-1";
		}
		
	}
	
	public static String getGoodsComboCode(){
		Date nowDate = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmsss");
		String now = sdf.format(nowDate);
		return String.format(now,(Math.random()*9+1)*100000);
	}
	

	public static String getStoreGoodsCode(){
		String format = "S%08d";
		int goodsCode = getMaxGoodsCode();
		goodsCode = goodsCode+1;
		return String.format(format, goodsCode);
	}
	
	public static String getOrderChangeCode(String code){
		int count = ordersDao.getOrderChangeCode(code);
		return "C"+code+"-"+count;
	}
	
	private static int getMaxGoodsCode(){
		return goodsDao.findMaxCode();
	}

	private static int getMaxTransactionHistoryReturnCode(String now){
		Pattern r = Pattern.compile("(\\d+)[a-zA-Z]+(\\d+)");
		String code = transactionHistoryDao.findMaxCode(now);
		if(code!=null){
			Matcher m = r.matcher(code);
			if(m.find()){
				return Integer.parseInt(m.group(2));
			}else{
				return 0;
			}
		}else{
			return 0;
		}
	}
	
	private static int getMaxTransactionHistoryCutCode(String now){
		Pattern r = Pattern.compile("(\\d+)[a-zA-Z]+(\\d+)");
		String code = transactionHistoryDao.findMaxCode(now);
		if(code!=null){
			Matcher m = r.matcher(code);
			if(m.find()){
				return Integer.parseInt(m.group(2));
			}else{
				return 0;
			}
		}else{
			return 0;
		}
	}
	
}
